package com.solocredit.finance.ui.main.adapter

import android.graphics.Color
import android.graphics.Typeface
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.solocredit.finance.R
import com.solocredit.finance.base.adapter.MullaRecyclerViewAdapter
import com.solocredit.finance.base.adapter.MullaViewHolderHelper
import com.solocredit.finance.utils.DateUtils
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/5 16:42
 * @Annotation：待还款列表适配器
 */

class MullaRepayAdapter(recyclerView: RecyclerView?) : MullaRecyclerViewAdapter<JSONObject>(recyclerView,R.layout.item_mulla_repay) {

    override fun setItemChildListener(helper: MullaViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        helper.setItemChildClickListener(R.id.detailsLayout) // 详情
    }

    override fun fillData(helper: MullaViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.textName,model.optString("name"))
        helper.setText(R.id.textPoints,String.format(mContext.getString(R.string.step_phonebook_add_points),model.optString("score")))
        helper.setText(R.id.textTotalAmount,"${model.optString("repayAmount")} ${model.optString("currency")}")
        val repaymentDate = model.optString("repaymentDate")
        val repaymentTimestamp = DateUtils.convertDateToTimestamp(repaymentDate)
        helper.setText(R.id.textDueDate,DateUtils.getFormatTime(repaymentTimestamp,"dd/MM/yyyy"))

        val textStatus = helper.getTextView(R.id.textStatus)
        //逾期时间
        val overdueDay = model.optString("overdueDay").toInt()
        if (overdueDay > 0){//逾期时长
            helper.setVisibility(R.id.statusLayout, View.VISIBLE)
            textStatus.text = String.format(mContext.getString(R.string.home_repayment_days_overdue),overdueDay.toString())
            textStatus.setTextColor(Color.parseColor("#FFFF4646"))
            textStatus.setTypeface(Typeface.DEFAULT,Typeface.BOLD)
        } else {//没有逾期
            // 还款时间 > 7天不显示，<= 7天显示
            val remainRepayDay = model.optInt("remainRepayDay")//剩余还款天数
            if (remainRepayDay > 7){
                helper.setVisibility(R.id.statusLayout, View.GONE)
            } else {
                helper.setVisibility(R.id.statusLayout, View.VISIBLE)
            }
            textStatus.text = String.format(mContext.getString(R.string.home_repayment_days_left),remainRepayDay.toString())
            textStatus.setTextColor(Color.parseColor("#FF666666"))
            textStatus.setTypeface(Typeface.DEFAULT,Typeface.NORMAL)
        }
    }
}